<html>

	<head>
		<title>Oracle NodeJs Tester</title>
		<link rel="stylesheet" type="text/css" href="assets/css/style.css"/>
		<script src="assets/js/jquery-1.8.3.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="assets/js/layer.js" type="text/javascript" charset="utf-8"></script>
		<script src="assets/js/common.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div class="tw-cdb-main">
			<dl>
				<dt>数据库配置[oracle]</dt>
				<dd style="text-align: right;">
					SID:<input type="text" name="o_sid" value="" /><br />
					user:<input type="text" name="o_user" value="" /><br />
					pwd:<input type="password" name="o_pwd" value="" /><br />
					<input type="button" value="读取配置" tg="readTns" />
					<input type="button" value="保存配置" tg="saveTns" />
					<input type="button" value="测试连接" tg="testTnsConn" />
					<textarea id="config"></textarea>
				</dd>
			</dl>
			<dl>
				<dt>SQL查询</dt>
				<dd>
					<input type="text" name="sqlExp" value="select * from tabs" />
					<input type="button" value="执行" tg="query" />
					<input type="button" value="重写" tg="rewrite" />
				</dd>
			</dl>
			<dl class="none">
				<dt>常用查询</dt>
				<dd>
					<input type="button" value="查询表名" tg="table" />
				</dd>
				<dd class="itemlist"></dd>
				<dd class="datalist"></dd>
			</dl>
			<dl>
				<dt>常用接口</dt>
				<dd>
					<input type="button" value="获取表" tg="getTableList" />
					<input type="button" value="清空记录" tg="clear" />
					<br>
					<input type="text" name="tableName" placeholder="表名" value="" /> <input type="button" value="获取字段" tg="getFieldList" />
					<br>
					<input type="text" name="fieldName" placeholder="字段名" value="" /> <input type="button" value="获取数据" tg="getData" />
				</dd>
				<dd class="itemlist"></dd>
				<dd class="datalist"></dd>
			</dl>
			<textarea id="result"></textarea>
		</div>
		<script type="text/javascript">
			var base = 'http://192.168.241.128:8999';
			var url = base+'/query';
			var url_saveTns = base+'/saveTns';
			var url_readTns = base+'/readTns';
			var url_testTns = base+'/testTnsConn';
			var br = '\r\n---------------------\r\n';
			
			/*table*/
			$('input[type=button][tg=table]').off('click').on('click',function(){
				var $this = $(this);
				var sql = "select * from tabs";//$this.prev('input[name=sqlExp]').val();
				getJSONData(url, {sqlExp:sql}, function(data){
					var items = [];
					$.each(data, function(i,e) {
						var name = e[0];
						$('#result').prepend(br+name);
						items.push('<item data-tableName="'+name+'">'+name+'</item>');
					});
					var $list = $this.parent().next('.itemlist');
					$list.empty().append(items.join(''));
					//列出字段
					bindItemClick();
				})
			})
			
			/*query*/
			$('input[type=button][tg=query]').off('click').on('click',function(){
				var $this = $(this);
				var sql = $this.prev('input[name=sqlExp]').val();
				getJSONData(url, {sqlExp:sql}, function(data){
					$('#result').prepend(br+JSON.stringify(data));
				})
			})
			
			/*readTns*/
			$('input[type=button][tg=readTns]').off('click').on('click',function(){
				var $this = $(this);
				var sid = $('input[name=o_sid]').val(),
					user = $('input[name=o_user]').val(),
					pwd = $('input[name=o_pwd]').val();
				var mi = layer.msg('读取中...',{icon:16,shade:0.3,time:0,offset:['200px']});
				getJSONData(url_readTns, {sid:sid,user:user,pwd:pwd}, function(data){
					//console.log(data);
					setTimeout(function(){
						layer.close(mi);
						if(data!=='NO'){
							$('#config').empty().append(data);
						}else{
							layer.alert('操作失败',{title:null,closeBtn:0,offset:['200px']})
						}
					}, 1000);
				})
			})
			
			/*saveTns*/
			$('input[type=button][tg=saveTns]').off('click').on('click',function(){
				var $this = $(this);
				var sid = $('input[name=o_sid]').val(),
					user = $('input[name=o_user]').val(),
					pwd = $('input[name=o_pwd]').val(),
					configTxt = $('#config').val();
				if($.trim(configTxt)==''){
					layer.alert('配置不能为空',{offset:['200px']});
					return false;
				}
				var mi = layer.msg('保存中...',{icon:16,shade:0.3,time:0,offset:['200px']});
				getJSONData(url_saveTns, {sid:sid,user:user,pwd:pwd,configTxt:configTxt}, function(data){
					//console.log(data);
					setTimeout(function(){
						layer.close(mi);
						if(data=='OK'){
							layer.alert('操作成功',{title:null,closeBtn:0,offset:['200px']})
						}else{
							layer.alert('操作失败',{title:null,closeBtn:0,offset:['200px']})
						}
					}, 1000);
				})
			})
			
			/*testTns*/
			$('input[type=button][tg=testTnsConn]').off('click').on('click',function(){
				var $this = $(this);
				var sid = $('input[name=o_sid]').val(),
					user = $('input[name=o_user]').val(),
					pwd = $('input[name=o_pwd]').val();
				var mi = layer.msg('测试连接中...',{icon:16,shade:0.3,time:0,offset:['200px']});
				getJSONData(url_testTns, {connectString:sid,user:user,password:pwd}, function(data){
					//console.log(data);
					setTimeout(function(){
						layer.close(mi);
						if(data=='OK'){
							layer.alert('连接成功',{title:null,closeBtn:0,offset:['200px']})
						}else{
							layer.alert('连接异常：'+data,{title:null,closeBtn:0,offset:['200px']})
						}
					}, 1000);
				})
			})
			
			
			/*clear*/
			$('input[type=button][tg=clear]').off('click').on('click',function(){
				$('#result').empty();
				$('.itemlist').empty();
			})
			$('input[type=button][tg=rewrite]').off('click').on('click',function(){
				$('input[name=sqlExp]').val('');
			})
			
			function bindItemClick(){
				$('item[data-tablename]').off('click').on('click', function(){
					var tableName = $(this).attr('data-tableName');
					var sql = "select column_name from all_tab_columns where table_name='" + tableName + "'";
					getJSONData(url, {sqlExp:sql}, function(data){
						var ths = [];
						$.each(data, function(i,e) {
							var name = JSON.stringify(e);
							$('#result').prepend(br+name);
							ths.push('<th>'+e+'</th>');
						});
						var $list = $('.datalist');
						var $table = $('<table class="tbw100 none"></table>');
						$table.append('<thead><tr>'+ths.join('')+'</tr></thead>');
						$list.empty().append($table[0].outerHTML);
						//datalist - query
					})
				})
			}
			
			
			
			
			
			/*接口列表*/
			$('input[type=button][tg=getTableList]').off('click').on('click',function(){
				var $this = $(this);
				var sql = "select * from tabs";//$this.prev('input[name=sqlExp]').val();
				getJSONData(url, {sqlExp:sql}, function(data){
					var items = [];
					$.each(data, function(i,e) {
						var name = e[0];
						$('#result').prepend(br+name);
					});
				})
			})
			$('input[type=button][tg=getFieldList]').off('click').on('click',function(){
				var $this = $(this);
				var tableName = $this.prev('input[name=tableName]').val();
				var sql = "select column_name from all_tab_columns where table_name='" + tableName + "'";
				getJSONData(url, {sqlExp:sql}, function(data){
					$.each(data, function(i,e) {
						var name = JSON.stringify(e);
						$('#result').prepend(br+name);
					});
				})
			})
			$('input[type=button][tg=getData]').off('click').on('click',function(){
				var $this = $(this);
				var tableName = $('input[name=tableName]').val();
				var fieldName = $('input[name=fieldName]').val();
				var sql = "select "+fieldName+" from "+tableName;
				getJSONData(url, {sqlExp:sql}, function(data){
					$('#result').prepend(br+JSON.stringify(data));
				})
			})
		</script>
	</body>

</html>